//
//  MemberCenterViewController.m
//  moon
//
//  Created by beyself on 16/6/20.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "MemberCenterViewController.h"
#import "PayViewController.h"

@interface MemberCenterViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation MemberCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会员中心";
    
    _mSelected = -1;

    UIImageView* imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test"]];
    imageview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/2);
    imageview.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:imageview];

    UITableView* tableview = [[UITableView alloc] init];
    tableview.backgroundColor = [UIColor clearColor];
    tableview.tag = 10000;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.frame = CGRectMake(0, SCREEN_WIDTH/2 + 10, SCREEN_WIDTH, SCREEN_HEIGHT - (SCREEN_WIDTH/2 + 20) - 64);
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableview];
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, SCREEN_HEIGHT - 64, SCREEN_WIDTH - 40, 48);
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT_15;
    [button setBackgroundImage:[Utils imageFromColor:RED] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button setTitle:@"续费" forState:UIControlStateNormal];
    [button setTitleColor:WHITE forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showDetail) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void) showDetail
{
    if(_mSelected == -1)
    {
        [self showAllTextDialog:@"请选择缴费的小孩"];
        return;
    }
    
    NSArray* child = APPDELEGATE.mMemberInfo[@"children"];
    NSDictionary* item = child[_mSelected];
    NSArray* classes = APPDELEGATE.mMemberInfo[@"classes"];
    
    NSMutableString* str = [NSMutableString stringWithCapacity:128];
    [str appendString:item[@"name"]];
    [str appendString:@"("];
    for(NSDictionary* dic in classes)
    {
        if([item[@"class_id"] intValue] == [dic[@"id"] intValue])
        {
            [str appendString:dic[@"school_name"]];
            NSString* className = dic[@"class_alias"];
            if([Utils isStringNullOrEmpty:className]) className = dic[@"class_name"];
            [str appendString:className];
            
            break;
        }
    }
    [str appendString:@")"];
    
    NSString* text = @"";
    int flag = 0;
    if(item[@"expire_date"] == nil || [item[@"expire_date"] isEqualToString:@"0"])
    {
        text = @"会员服务已到期，点击续费";
    }
    else
    {
        NSDate* date = [Utils string2Date:item[@"expire_date"] format:@"yyyy-MM-dd HH:mm:ss"];
        NSString* dateStr = [Utils date2String:date format:@"yyyy年MM月dd日"];
        text = [NSString stringWithFormat:@"会员服务于%@到期",dateStr];
        
        if([date compare:[NSDate date]] > 0)
        {
            flag = 1;
        }
    }
    
    PayViewController* subview = [[PayViewController alloc] init];
    subview.childId = [item[@"student_id"] intValue];
    subview.mTitle = str;
    subview.mSubTitle = text;
    subview.mFlag = flag;
    subview.mExpireDate = item[@"expire_date"];
    [self.navigationController pushViewController:subview animated:YES];
}

- (void) viewWillAppear:(BOOL)animated
{
    [(UITableView*)[self.view viewWithTag:10000] reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray* child = APPDELEGATE.mMemberInfo[@"children"];
    return child.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        cell.backgroundColor = WHITE;
        
        UIImageView* imageview = [[UIImageView alloc] init];
        imageview.frame = CGRectMake(10, 10, 44, 44);
        imageview.layer.cornerRadius = 22;
        imageview.layer.masksToBounds = YES;
        imageview.image = [UIImage imageNamed:@"ic_child"];
        [cell addSubview:imageview];
        
        UILabel* label = [[UILabel alloc] init];
        label.frame = CGRectMake(64, 10, SCREEN_WIDTH - 84, 22);
        label.tag = 1000;
        label.textColor = BLUE;
        label.font = FONT_14;
        [cell addSubview:label];
        
        label = [[UILabel alloc] init];
        label.frame = CGRectMake(64, 32, SCREEN_WIDTH - 100, 22);
        label.tag = 1001;
        label.textColor = RED;
        label.font = FONT_14;
        [cell addSubview:label];
        
        UIView* subview = [[UIView alloc] init];
        subview.frame = CGRectMake(SCREEN_WIDTH - 32, 0, 32, 64);
        subview.backgroundColor = WHITE;
        subview.tag = 1002;
        [cell addSubview:subview];
        
        CALayer* layer = [[CALayer alloc] init];
        layer.frame = CGRectMake(0, 63.5, SCREEN_WIDTH, .5);
        layer.backgroundColor = BG.CGColor;
        [cell.layer addSublayer:layer];
    }
    
    NSArray* child = APPDELEGATE.mMemberInfo[@"children"];
    NSArray* classes = APPDELEGATE.mMemberInfo[@"classes"];
    
    NSDictionary* item = child[indexPath.row];
    NSMutableString* str = [NSMutableString stringWithCapacity:128];
    [str appendString:item[@"name"]];
    [str appendString:@"("];
    for(NSDictionary* dic in classes)
    {
        if([item[@"class_id"] intValue] == [dic[@"id"] intValue])
        {
            [str appendString:dic[@"school_name"]];
            NSString* className = dic[@"class_alias"];
            if([Utils isStringNullOrEmpty:className]) className = dic[@"class_name"];
            [str appendString:className];
            
            break;
        }
    }
    [str appendString:@")"];

    ((UILabel*)[cell viewWithTag:1000]).text = str;
    
    ((UILabel*)[cell viewWithTag:1001]).textColor = RED;
    if(item[@"expire_date"] == nil || [item[@"expire_date"] isEqualToString:@"0"])
    {
        ((UILabel*)[cell viewWithTag:1001]).text = @"会员服务已到期，点击续费";
    }
    else
    {
        NSDate* date = [Utils string2Date:item[@"expire_date"] format:@"yyyy-MM-dd HH:mm:ss"];
        NSString* dateStr = [Utils date2String:date format:@"yyyy年MM月dd日"];
        ((UILabel*)[cell viewWithTag:1001]).text = [NSString stringWithFormat:@"会员服务于%@到期",dateStr];
        
        if([date compare:[NSDate date]] > 0)
        {
            ((UILabel*)[cell viewWithTag:1001]).textColor = BLUE;
        }
    }
    
    UIView* subview = [cell viewWithTag:1002];
    subview.backgroundColor = WHITE;
    if(_mSelected == indexPath.row) subview.backgroundColor = GREEN;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _mSelected = indexPath.row;
    [tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
